/*
Copyright (c) 2012, Janez Žemva
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
o The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ETL_MATRIX_HPP
# define ETL_MATRIX_HPP
# pragma once

#include "vector.hpp"

namespace etl
{

template <typename, ::std::size_t, ::std::size_t, typename>
struct matrix_expression;

template <typename T, ::std::size_t M, ::std::size_t N>
struct matrix : matrix_expression<T, M, N, matrix<T, M, N> >
{
  using value_type = T;
  using size_type = ::std::size_t;

  using difference_type = ::std::ptrdiff_t;

  using iterator = T*;
  using const_iterator = T const*;

  using reverse_iterator = ::std::reverse_iterator<iterator>;
  using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;

  constexpr matrix() = default;

  constexpr matrix(matrix const&) = default;
  constexpr matrix(matrix&&) = default;

  template <typename U,
    typename = typename ::std::enable_if<
      !::std::is_same<matrix, typename ::std::decay<U>::type>{}
    >::type
  >
  matrix(U&& u)
  {
    *this = ::std::forward<U>(u);
  }

  template <typename ...U,
    typename = typename ::std::enable_if<detail::all_of<
      ::std::is_same<T, typename ::std::decay<U>::type>...>{} &&
      (M * N == sizeof...(U))
    >::type
  >
  matrix(U&&... args)
  {
    static_assert((M * N == sizeof...(U)),
      "wrong number of arguments in assignment");

    assign_constants(detail::make_cartesian<M, N>(),
      ::std::forward<U>(args)...);
  }

  template <typename E>
  matrix(matrix_expression<T, M, N, E>&& other)
  {
    *this = ::std::forward<E>(other);
  }

  constexpr const_iterator cbegin() const { return &data_[0][0]; }
  constexpr const_iterator cend() const { return &data_[0][0] +
    M * detail::map_extent<N>{}; }

  iterator begin() { return &data_[0][0]; }
  iterator end() { return &data_[0][0] + M * detail::map_extent<N>{}; }

  constexpr const_iterator begin() const { return &data_[0][0]; }
  constexpr const_iterator end() const { return &data_[0][0] +
    M * detail::map_extent<N>{}; }

  constexpr value_type const* cdata() const { return &data_[0][0]; }

  value_type* data() { return &data_[0][0]; }
  constexpr value_type const* data() const { return &data_[0][0]; }

  static constexpr size_type size() noexcept { return M * N; }

  matrix& operator=(matrix const&) = default;
  matrix& operator=(matrix&&) = default;

  matrix& operator=(T const& v)
  {
    assign_constant(v, detail::make_cartesian<M, N>());

    return *this;
  }

  matrix& operator=(::std::initializer_list<T> list)
  {
    assert(size() == list.size());
    assign_initializer<::std::initializer_list<T> >(
      ::std::begin(list), detail::make_cartesian<M, N>());

    return *this;
  }

  template <class Container>
  typename ::std::enable_if<
    detail::is_container<T, Container>{},
    matrix&
  >::type
  operator=(Container const& container)
  {
    assert(size() ==
      ::std::distance(::std::begin(container), ::std::end(container)));
    assign_initializer<Container>(
      ::std::begin(container), detail::make_cartesian<M, N>());

    return *this;
  }

  template <typename E>
  matrix& operator=(matrix_expression<T, M, N, E> const& rhs)
  {
    assign_expression(rhs, detail::make_cartesian<M, N>());

    return *this;
  }

  template <typename E>
  matrix& operator+=(matrix_expression<T, M, N, E> const& rhs)
  {
    add_expression(rhs, detail::make_cartesian<M, N>());

    return *this;
  }

  template <typename E>
  matrix& operator-=(matrix_expression<T, M, N, E> const& rhs)
  {
    subtract_expression(rhs, detail::make_cartesian<M, N>());

    return *this;
  }

  matrix& operator*=(value_type const s)
  {
    scalar_multiply(s, detail::make_cartesian<M, N>());

    return *this;
  }

  constexpr T cget(size_type const i, size_type const j) const
  {
    return data_[i][j]; 
  }

  T const& get(size_type const i, size_type const j) const
  {
    return data_[i][j];
  }

  T& get(size_type const i, size_type const j) { return data_[i][j]; }

  constexpr T operator()(size_type const i, size_type const j) const
  {
    return data_[i][j]; 
  }

  T& operator()(size_type const i, size_type const j) { return data_[i][j]; }

  template<typename ...U>
  typename ::std::enable_if<
    detail::all_of<::std::is_same<
      typename ::std::decay<U>::type, T>...
    >{}
  >::type
  assign(U&& ...args)
  {
    static_assert((M * N == sizeof...(U)),
      "wrong number of arguments in assignment");

    assign_constants(detail::make_cartesian<M, N>(),
      ::std::forward<U>(args)...);
  }

private:
  template <typename ...Ps>
  void assign_constant(T const& v, detail::list<Ps...> const)
  {
    [](...){ }((data_[typename Ps::first{}][typename Ps::second{}] =
      v, 0)...);
  }

  template<class ...U, typename ...Ps>
  void assign_constants(detail::list<Ps...> const, U&& ...args)
  {
    [](...){ }((data_[typename Ps::first{}][typename Ps::second{}] =
      ::std::forward<U>(args), 0)...);
  }

  template <typename C, typename ...Ps>
  void assign_initializer(typename C::const_iterator v,
    detail::list<Ps...> const)
  {
    ::std::initializer_list<int>{(
      data_[typename Ps::first{}][typename Ps::second{}] = *v++, 0)...};
  }

  template<typename ...Ps, class E>
  void assign_expression(matrix_expression<T, M, N, E> const& e,
    detail::list<Ps...> const)
  {
    [](...){ }((data_[typename Ps::first{}][typename Ps::second{}] =
      e(typename Ps::first{}, typename Ps::second{}), 0)...);
  }

  template<typename ...Ps, class E>
  void add_expression(matrix_expression<T, M, N, E> const& e,
    detail::list<Ps...> const)
  {
    [](...){ }((data_[typename Ps::first{}][typename Ps::second{}] +=
      e(typename Ps::first{}, typename Ps::second{}), 0)...);
  }

  template<typename ...Ps, class E>
  void subtract_expression(matrix_expression<T, M, N, E> const& e,
    detail::list<Ps...> const)
  {
    [](...){ }((data_[typename Ps::first{}][typename Ps::second{}] -=
      e(typename Ps::first{}, typename Ps::second{}), 0)...);
  }

  template<typename ...Ps>
  void scalar_multiply(T const s,
    detail::list<Ps...> const)
  {
    [](...){}((data_[typename Ps::first{}][typename Ps::second{}] *=
      s, 0)...);
  }

private:
#if (__ARM_NEON__) || defined(__SSE__) || defined(__SSE2__) ||\
  defined(__GNUC__)
  alignas(16) T data_[M][detail::map_extent<N>{}];
#else
  T data_[M][N];
#endif
};

template <typename>
struct is_matrix : ::std::false_type { }; 

template <typename T, ::std::size_t M, ::std::size_t N>
struct is_matrix<matrix<T, M, N> > : ::std::true_type { };

template <typename vec_type, ::std::size_t ...Is, typename T,
  ::std::size_t M, ::std::size_t N, typename E>
inline typename ::std::enable_if<!is_matrix<E>{}, vec_type>::type
get_row(matrix_expression<T, M, N, E> const& e, ::std::size_t const row)
{
  return vec_type{e(row, Is)...};
}

template <typename vec_type, ::std::size_t ...Is, typename T,
  ::std::size_t M, ::std::size_t N, typename E>
inline typename ::std::enable_if<is_matrix<E>{}, vec_type const&>::type
get_row(matrix_expression<T, M, N, E> const& e, ::std::size_t const row)
{
  return *reinterpret_cast<vec_type const*>(
    &(static_cast<matrix<T, M, N> const&>(e).get(row, 0)));
}

template <typename T, ::std::size_t M, ::std::size_t N, typename E>
struct matrix_expression 
{
public:
  using value_type = T;
  using size_type = ::std::size_t;

  using reference = value_type&;
  using const_reference = value_type const&;

  static constexpr auto rows = M;
  static constexpr auto cols = N;

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return static_cast<E const&>(*this)(i, j);
  }

  operator E&() { return static_cast<E&>(*this); }

  constexpr operator E const&() const { return static_cast<E const&>(*this); }
};

template <typename E>
struct identity :
  matrix_expression<typename E::value_type, E::rows, E::rows, identity<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr identity() = default;

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    static_assert(E::rows == E::cols, "rows != cols");

    return value_type(i == j);
  }
};

template <typename T, ::std::size_t N>
struct identity_matrix : matrix_expression<T, N, N, identity_matrix<T, N> >
{
  using value_type = T;
  using size_type = ::std::size_t;

  constexpr identity_matrix() = default;

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return value_type(i == j);
  }
};

template <typename E>
struct matrix_column :
  vector_expression<typename E::value_type, E::rows, matrix_column<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr matrix_column(E const& u, size_type const j) : u_(u), j_(j) { }

  constexpr value_type operator()(size_type const i) const
  {
    return u_(i, j_);
  }

  private:
    E const& u_;
    size_type j_;
};

template <typename E>
struct matrix_row :
  vector_expression<typename E::value_type, E::cols, matrix_row<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr matrix_row(E const& u, size_type i) : u_(u), i_(i) { }

  constexpr value_type operator()(size_type const j) const
  {
    return u_(i_, j);
  }

  private:
    E const& u_;
    size_type i_;
};

template <typename E>
struct matrix_trans : matrix_expression<typename E::value_type, E::rows,
  E::cols, matrix_trans<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr matrix_trans(E const& u) : u_(u) { }

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return u_(j, i);
  }

  private:
    E const& u_;
};

template <typename E>
struct matrix_negation : matrix_expression<typename E::value_type, E::rows,
  E::cols, matrix_negation<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr matrix_negation(E const& u) : u_(u) { }

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return -u_(i, j);
  }

  private:
    E const& u_;
};

template <typename E1, typename E2>
struct matrix_addition : matrix_expression<
  sum_type<typename E1::value_type, typename E2::value_type>,
  E1::rows, E1::cols, matrix_addition<E1, E2> >
{
  using value_type = sum_type<typename E1::value_type,
    typename E2::value_type>;
  using size_type = typename E1::size_type;

  constexpr matrix_addition(E1 const& u, E2 const& v) : u_(u), v_(v) { }

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return u_(i, j) + v_(i, j);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <typename E1, typename E2>
struct matrix_subtraction : matrix_expression<
  diff_type<typename E1::value_type, typename E2::value_type>,
  E1::rows, E1::cols, matrix_subtraction<E1, E2> >
{
  using size_type = typename E1::size_type;

  using value_type = diff_type<typename E1::value_type,
    typename E2::value_type>;

  constexpr matrix_subtraction(E1 const& u, E2 const& v) : u_(u), v_(v) { }

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return u_(i, j) - v_(i, j);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <typename E1, typename E2>
struct matrix_multiplication : matrix_expression<
  prod_type<typename E1::value_type, typename E2::value_type>,
  E1::rows, E1::cols, matrix_multiplication<E1, E2> >
{
  using size_type = typename E1::size_type;

  using value_type =
    prod_type<typename E1::value_type, typename E2::value_type>;

  constexpr matrix_multiplication(E1 const& u, E2 const& v) :
    u_(u),
    v_(v)
  {
    static_assert(E1::cols == E2::rows, "E1::cols != E2::rows");
  }

  constexpr value_type operator()(size_type const i, size_type const j) const
  {
    return get(i, j, detail::make_indices<E1::cols>());
  }

private:
#if defined(__GNUC__) && !defined(__clang__)
  template <size_type, size_type... Is>
  value_type get(size_type const row, size_type const col) const
  {
    typedef value_type vec_type __attribute__ ((vector_size(
      detail::map_extent<E1::cols>{} * sizeof(value_type))));

    vec_type const v(get_row<vec_type, 0, Is...>(u_, row) *
      vec_type{v_(0, col), v_(Is, col)...});

    value_type r(v[0]);

    ::std::initializer_list<int>{(r += v[Is], 0)...};

    return r;
  }
#else
  template <size_type j, size_type... Is>
  constexpr typename ::std::enable_if<
    bool(!sizeof...(Is)), value_type
  >::type
  get(size_type const row, size_type const col) const
  {
    return u_(row, j) * v_(j, col);
  }

  template <size_type j, size_type... Is>
  constexpr typename ::std::enable_if<
    bool(sizeof...(Is)), value_type
  >::type
  get(size_type const row, size_type const col) const
  {
    return u_(row, j) * v_(j, col) + get<Is...>(row, col);
  }
#endif

  template<size_type... Is>
  value_type get(size_type const row, size_type const col,
    detail::indices<Is...> const) const
  {
    return get<Is...>(row, col);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <typename E1, typename E2>
struct matrix_vector_multiplication : vector_expression<
  prod_type<typename E1::value_type, typename E2::value_type>,
  E2::dimension, matrix_vector_multiplication<E1, E2> >
{
  using value_type = prod_type<typename E1::value_type,
    typename E2::value_type>;
  using size_type = typename E1::size_type;

  constexpr matrix_vector_multiplication(E1 const& u, E2 const& v) :
    u_(u),
    v_(v)
  {
    static_assert(E1::cols == E2::dimension,
      "E1::cols != E2::dimension");
  }

  constexpr value_type operator()(size_type const i) const
  {
    return get(i, detail::make_indices<E2::dimension>());
  }

private:
#if defined(__GNUC__) && !defined(__clang__)
  template <size_type, size_type... Is>
  value_type get(size_type const row) const
  {
    typedef value_type vec_type __attribute__ ((vector_size(
      detail::map_extent<E1::cols>{} * sizeof(value_type))));

    vec_type const v(get_row<vec_type, 0, Is...>(u_, row) *
      vec_type{v_(0), v_(Is)...});

    value_type r(v[0]);

    ::std::initializer_list<int>{(r += v[Is], 0)...};

    return r;
  }
#else
  template <size_type i, size_type... Is>
  constexpr value_type get(size_type const row,
    typename ::std::enable_if<sizeof...(Is) == 0>::type* = 0) const
  {
    return u_(row, i) * v_(i);
  }

  template <size_type i, size_type... Is>
  constexpr value_type get(size_type const row,
    typename ::std::enable_if<sizeof...(Is) != 0>::type* = 0) const
  {
    return u_(row, i) * v_(i) + get<Is...>(row);
  }
#endif

  template<size_type... Is>
  constexpr value_type get(size_type const row,
    detail::indices<Is...> const) const
  {
    return get<Is...>(row);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <typename E1, typename E2>
struct vector_matrix_multiplication : vector_expression<
  prod_type<typename E1::value_type, typename E2::value_type>,
  E1::dimension, vector_matrix_multiplication<E1, E2> >
{
  using value_type = prod_type<typename E1::value_type,
    typename E2::value_type>;
  using size_type = typename E1::size_type;

  constexpr vector_matrix_multiplication(E1 const& u, E2 const& v) :
    u_(u),
    v_(v)
  {
    static_assert(E1::dimension == E2::rows,
      "E1::dimension != E2::rows");
  }

  constexpr value_type operator()(size_type const j) const
  {
    return get(j, detail::make_indices<E1::cols>());
  }

private:
#if defined(__GNUC__) && !defined(__clang__)
  template <size_type, size_type... Is>
  value_type get(size_type const col) const
  {
    typedef value_type vec_type __attribute__ ((vector_size(
      detail::map_extent<E1::cols>{} * sizeof(value_type))));

    vec_type const v(get_vec<vec_type, 0, Is...>(u_) *
      vec_type{v_(0, col), v_(Is, col)...});

    value_type r(v[0]);

    ::std::initializer_list<int>{(r += v[Is], 0)...};

    return r;
  }
#else
  template <size_type i, size_type... Is>
  constexpr typename ::std::enable_if<
    bool(!sizeof...(Is)), value_type
  >::type
  get(size_type const col) const
  {
    return u_(i) * v_(i, col);
  }

  template <size_type i, size_type... Is>
  constexpr typename ::std::enable_if<
    bool(sizeof...(Is)), value_type
  >::type
  get(size_type const col) const
  {
    return u_(i) * v_(i, col) + get<Is...>(col);
  }
#endif

  template<size_type... Is>
  constexpr value_type get(size_type const col,
    detail::indices<Is...> const) const
  {
    return get<Is...>(col);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <class E>
constexpr inline matrix_column<E>
column(matrix_expression<typename E::value_type,
  E::rows, E::cols, E> const& rhs,
  typename E::size_type j)
{
  return matrix_column<E>(rhs, j);
}

template <class E>
constexpr inline matrix_row<E>
row(matrix_expression<typename E::value_type,
  E::rows, E::cols, E> const& rhs,
  typename E::size_type i)
{
  return matrix_row<E>(rhs, i);
}

template <class E>
constexpr inline matrix_trans<E>
trans(matrix_expression<typename E::value_type,
  E::rows, E::cols, E> const& rhs)
{
  return matrix_trans<E>(rhs);
}

template <class E>
constexpr inline matrix_negation<E>
operator-(matrix_expression<typename E::value_type,
  E::rows, E::cols, E> const& rhs)
{
  return matrix_negation<E>(rhs);
}

template <class E1, class E2>
constexpr inline matrix_addition<E1, E2>
operator+(matrix_expression<typename E1::value_type,
    E1::rows, E1::cols, E1> const& lhs,
  matrix_expression<typename E2::value_type,
    E2::rows, E2::cols, E2> const& rhs)
{
  return matrix_addition<E1, E2>(lhs, rhs);
}

template <class E1, class E2>
constexpr inline matrix_subtraction<E1, E2>
operator-(matrix_expression<typename E1::value_type,
    E1::rows, E1::cols, E1> const& lhs,
  matrix_expression<typename E2::value_type,
    E2::rows, E2::cols, E2> const& rhs)
{
  return matrix_subtraction<E1, E2>(lhs, rhs);
}

template <class E1, class E2>
constexpr inline matrix_multiplication<E1, E2>
operator*(matrix_expression<typename E1::value_type,
    E1::rows, E1::cols, E1> const& lhs,
  matrix_expression<typename E2::value_type,
    E2::rows, E2::cols, E2> const& rhs)
{
  return matrix_multiplication<E1, E2>(lhs, rhs);
}

template <class E1, class E2>
constexpr inline matrix_vector_multiplication<E1, E2>
operator*(matrix_expression<typename E1::value_type,
    E1::rows, E1::cols, E1> const& lhs,
  vector_expression<typename E2::value_type,
    E1::rows, E2> const& rhs)
{
  return matrix_vector_multiplication<E1, E2>(lhs, rhs);
}

template <class E1, class E2>
constexpr inline vector_matrix_multiplication<E1, E2>
operator*(vector_expression<typename E1::value_type,
  E2::cols, E1> const& lhs,
  matrix_expression<typename E2::value_type,
  E2::rows, E2::cols, E2> const& rhs)
{
  return vector_matrix_multiplication<E1, E2>(lhs, rhs);
}

#if defined(__GNUC__) && !defined(__clang__)
namespace matrix_eq_impl
{
  template <::std::size_t i, ::std::size_t ...Is, std::size_t ...Js,
    typename E1, typename E2>
  inline typename ::std::enable_if<!bool(sizeof...(Is)), bool>::type
  get(E1 const& lhs, E2 const& rhs)
  {
    using value_type = typename E1::value_type;

    typedef value_type vec_type __attribute__ ((vector_size(
      detail::map_extent<E1::cols>{} * sizeof(value_type))));

    vec_type const v(get_row<vec_type, 0, (Js + 1)...>(lhs, i) ==
      get_row<vec_type, 0, (Js + 1)...>(rhs, i));

    bool r(v[0]);

    ::std::initializer_list<int>{(r = r && v[Js + 1])...};

    return r;
  }

  template <::std::size_t i, ::std::size_t ...Is, std::size_t ...Js,
    typename E1, typename E2>
  inline typename ::std::enable_if<bool(sizeof...(Is)), bool>::type
  get(E1 const& lhs, E2 const& rhs)
  {
    using value_type = typename E1::value_type;

    typedef value_type vec_type __attribute__ ((vector_size(
      detail::map_extent<E1::cols>{} * sizeof(value_type))));

    auto const v(get_row<vec_type, 0, (Js + 1)...>(lhs, i) ==
      get_row<vec_type, 0, (Js + 1)...>(rhs, i));

    bool r(v[0]);

    ::std::initializer_list<int>{(r = r && v[Js + 1])...};

    return r && get<Is..., Js...>(lhs, rhs);
  }

  template <typename E1, typename E2, ::std::size_t ...Is,
    ::std::size_t ...Js>
  inline constexpr typename E1::value_type
  get(E1 const& lhs, E2 const& rhs, detail::indices<Is...> const,
    detail::indices<Js...>)
  {
    static_assert(E1::rows == sizeof...(Is), "internal error");

    return get<Is..., Js...>(lhs, rhs);
  }
}

template <typename E1, typename E2>
inline constexpr bool
operator==(matrix_expression<typename E1::value_type,
  E1::rows, E1::cols, E1> const& lhs,
  matrix_expression<typename E2::value_type,
  E2::rows, E2::cols, E2> const& rhs)
{
  return (E1::rows == E2::rows) && (E1::cols == E2::cols) ?
    matrix_eq_impl::get<E1, E2>(lhs, rhs, detail::make_indices<E1::rows>(),
      detail::make_indices<E1::cols - 1>()) :
    false;
}
#else
namespace matrix_eq_impl
{
  template <typename P, typename... Ps, typename E1, typename E2>
  inline constexpr typename ::std::enable_if<bool(!sizeof...(Ps)), bool>::type
  get(E1 const& lhs, E2 const& rhs)
  {
    return lhs(typename P::first{}, typename P::second{}) ==
      rhs(typename P::first{}, typename P::second{});
  }

  template <typename P, typename... Ps, typename E1, typename E2>
  inline constexpr typename ::std::enable_if<bool(sizeof...(Ps)), bool>::type
  get(E1 const& lhs, E2 const& rhs)
  {
    return lhs(typename P::first{}, typename P::second{}) ==
      rhs(typename P::first{}, typename P::second{}) &&
      get<Ps...>(lhs, rhs);
  }

  template <typename E1, typename E2, typename ...Ps>
  inline constexpr typename E1::value_type
  get(E1 const& lhs, E2 const& rhs, detail::list<Ps...> const)
  {
    static_assert(E1::rows * E1::cols == sizeof...(Ps), "internal error");
    return get<Ps...>(lhs, rhs);
  }
}

template <typename E1, typename E2>
inline constexpr bool
operator==(matrix_expression<typename E1::value_type,
  E1::rows, E1::cols, E1> const& lhs,
  matrix_expression<typename E2::value_type,
  E2::rows, E2::cols, E2> const& rhs)
{
  return (E1::rows == E2::rows) && (E1::cols == E2::cols) ?
    matrix_eq_impl::get(lhs, rhs,
      detail::make_cartesian<E1::rows, E1::cols>()) :
    false;
}
#endif

template <typename E1, typename E2>
inline constexpr bool
operator!=(matrix_expression<typename E1::value_type,
  E1::rows, E1::cols, E1> const& lhs,
  matrix_expression<typename E1::value_type,
  E2::rows, E2::cols, E2> const& rhs)
{
  return !operator==(lhs, rhs);
}

template <typename E1>
::std::ostream& operator<<(::std::ostream& os,
  matrix_expression<typename E1::value_type, E1::rows,
    E1::cols, E1> const& u)
{
  for (typename E1::size_type i{}; i != E1::rows - 1; ++i)
  {
    os << (i ? ' ' : '[');

    for (typename E1::size_type j{}; j != E1::cols - 1; ++j)
    {
      os << u(i, j) << ", ";
    }

    os << u(i, E1::cols - 1) << '\n';
  }

  os << ' ';

  for (typename E1::size_type j{}; j != E1::cols - 1; ++j)
  {
    os << u(E1::rows - 1, j) << ", ";
  }

  os << u(E1::rows - 1, E1::cols - 1) << ']';

  return os;
}

}

#endif // ETL_MATRIX_HPP
